package action;

import controller.Action;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.Curso;
import model.Disciplina;

public abstract class ManterDisciplinaAction extends Action{
  
    @Override
    public void prepararOperacao(HttpServletRequest request, HttpServletResponse response) throws ServletException {
        try {
            String action = request.getParameter("action");
            request.setAttribute("action", action);
            setParametros(request, response);
            request.setAttribute("cursos", Curso.obterCursos());
            List<Disciplina> disciplinas = Disciplina.obterDisciplinas();
            request.setAttribute("preRequisitos", disciplinas);
            RequestDispatcher view = request.getRequestDispatcher("/manterDisciplina.jsp");
            view.forward(request, response);
        } catch (ServletException e) {
            throw e;
        } catch (IOException e) {
            throw new ServletException(e);
        } catch (SQLException e) {
            throw new ServletException(e);
        } catch (ClassNotFoundException e) {
            throw new ServletException(e);
        }
    }

    @Override
    public void confirmarOperacao(HttpServletRequest request, HttpServletResponse response) throws ServletException {
        try {
            int codDisciplina = Integer.parseInt(request.getParameter("codDisciplina"));
            int periodo = Integer.parseInt(request.getParameter("periodo"));
            int codCurso = Integer.parseInt(request.getParameter("curso"));
            String nomeDisciplina = request.getParameter("nomeDisciplina");
            int qtdCreditos = Integer.parseInt(request.getParameter("qtdCreditos"));
            String ementa = request.getParameter("ementa");
            Set<Disciplina> preRequisitos = new HashSet<Disciplina>();
            List<String> elements = Arrays.asList(request.getParameter("preRequisitosSerialized").split(","));

            for (String elemento : elements) {
                if (!elemento.equals("")) {
                    preRequisitos.add(Disciplina.obterDisciplina(Integer.parseInt(elemento)));
                }
            }
            Curso curso = Curso.obterCurso(codCurso);

            Disciplina disciplina = new Disciplina(codDisciplina, nomeDisciplina, ementa, periodo, qtdCreditos, curso, preRequisitos);
            efetuaPersistencia(disciplina);

            RequestDispatcher view = request.getRequestDispatcher("FrontController?action=PesquisarDisciplina&filtro=0");
            view.forward(request, response);
        } catch (ServletException e) {
            throw e;
        } catch (IOException e) {
            throw new ServletException(e);
        } catch (SQLException e) {
            throw new ServletException(e);
        } catch (ClassNotFoundException e) {
            throw new ServletException(e);
        }
    }
    
    public abstract void setParametros(HttpServletRequest request, HttpServletResponse response) throws ServletException;

    public abstract void efetuaPersistencia(Disciplina disciplina) throws ServletException;
    
}
